package com.frontop.bigdata.task;

import com.frontop.bigdata.modules.model.BigDataProject;
import com.frontop.bigdata.modules.service.BigDataAccessPageService;
import com.frontop.bigdata.modules.service.BigDataOverviewService;
import com.frontop.bigdata.modules.service.BigDataProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author YangBoos
 * @title: BaiduPullTask
 * @projectName frontop-cloud
 * @description: 百度大数据拉取任务
 * @date 2022/2/11 17:27
 */
@Component
@Slf4j
@EnableScheduling
//默认条件注解是开启的，采用配置文件的变量来手动控制定时任务是否执行
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
@PropertySource("classpath:bootstrap.yml")
public class BaiduPullTask {
    @Autowired
    BigDataProjectService bigDataProjectServiceb;

    @Autowired
    BigDataAccessPageService bigDataAccessPageService;

    @Autowired
    BigDataOverviewService bigDataOverviewService;

    /**
     * 每天23点执行一次：拉取站点列表
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void pullProjectList() {
        bigDataProjectServiceb.pullProjectList();
    }

    /**
     * 每小时0 30,59分钟执行一次：获取当天所有页面的访问数据统计
     */
    @Scheduled(cron = "0 30,59 * * * ?")
    //@Scheduled(cron = "*/30 * * * * ?")
    public void pullAccessPageData() {
        /*
        Calendar   cal   =   Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        String date = new SimpleDateFormat( "yyyyMMdd").format(cal.getTime());
        */
        List<BigDataProject> bigDataProjectList = bigDataProjectServiceb.list();
            String date = new SimpleDateFormat( "yyyyMMdd").format(new Date());
            for (BigDataProject bigDataProject : bigDataProjectList){
                bigDataAccessPageService.pullAccessPageData(bigDataProject.getSiteId(),date);
            }
    }

    /**
     * 每小时20,40,59分钟执行一次：获取当日所有站点的总览数据
     */
   @Scheduled(cron = "0 20,40,59 * * * ?")
    //@Scheduled(cron = "*/30 * * * * ?")
    public void pullBigDataOverview() {
        List<BigDataProject> bigDataProjectList = bigDataProjectServiceb.list();
        String date = new SimpleDateFormat( "yyyyMMdd").format(new Date());
        for (BigDataProject bigDataProject : bigDataProjectList){
           bigDataOverviewService.pullDataOverview(bigDataProject.getSiteId(),date);
        }
    }
}
